﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.ComponentModel;
using PhoneInfo.Converters;

namespace PhoneInfo
{
    public class LexunContentBox : System.Windows.Controls.RichTextBox
    {
        /// <summary>
        /// 设置DependencyProperty属性
        /// </summary>
        public DependencyProperty ContentProperty;
        /// <summary>
        /// 设置Content的访问器
        /// </summary>
        public string Content
        {
            set
            {
                SetValue(ContentProperty, value);
            }
            get
            {
                return (string)GetValue(ContentProperty);
            }
        }

        public LexunContentBox()
        {
            //设置DependencyProperty的默认描述
            PropertyMetadata _metadata = new PropertyMetadata(string.Empty, OnContentChanged); ;
            ////设置默认值勤
            //_metadata.DefaultValue = null;
            ////设置是否影响布局
            //_metadata.AffectsMeasure = true;
            ////设置是否影响到其子控件
            //_metadata.Inherits = true;
            ////设置属性变化时的处理方法
            //_metadata.PropertyChangedCallback += OnImagePropertyChanged;

            ContentProperty = DependencyProperty.Register("Content", typeof(string), typeof(LexunContentBox), _metadata);
            Content = this.Xaml;
        }

        /// <summary>
        /// 设置回调方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        void OnContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            try
            {
                string content = (string)args.NewValue;
                //&#215;
                content = content.Replace("&#215;", "*");
                Paragraph parag = new Paragraph();

                parseContent(parag, content);
                this.Blocks.Clear();
                this.Blocks.Add(parag);
               
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception in Custom.RichTextBox.OnContentChanged:" + e.Message);
            }
        }
        private void parseContent(Paragraph parag, string content)
        {
            if (content == null || content.Length <= 0)
            {
                return;
            }

            int start = content.IndexOf("<a");

           
            if (start < 0)
            {

                Run run = new Run();
                run.Text = content.TrimEnd();
                run.FontSize = 24;
                run.Foreground = Theme.cuTheme.SecondTextBrush;
                parag.Inlines.Add(run);
                //Blocks.Add(parag);
                return;
            }
            int end = content.LastIndexOf("a>");
            if (end < 0)
            {

                Run run = new Run();

                run.Foreground = Theme.cuTheme.SecondTextBrush;
                run.FontSize = 24;
                run.Text = content.TrimEnd(); ;
                parag.Inlines.Add(run);
                //Blocks.Add(parag);
                return;
            }
            Run startrun = new Run();
            startrun.Foreground = Theme.cuTheme.SecondTextBrush;
            startrun.FontSize = 24;
            startrun.Text = content.Substring(0, start);
            parag.Inlines.Add(startrun);
            parsSuperUrl(content.Substring(start, end + 2 - start), parag);

            Run endrun = new Run();
            endrun.Foreground = Theme.cuTheme.SecondTextBrush;
            endrun.FontSize = 24;
            endrun.Text = content.Substring(end + 2, content.Length - (end + 2));
            parag.Inlines.Add(endrun);
            //Blocks.Add(parag);

        }
        private static void parsSuperUrl(string url, Paragraph parag)
        {

            int start = url.IndexOf("<a");
            
            while (start >= 0)
            {
                int attributestart = url.IndexOf("href=", start);
                int attributeend = url.IndexOf(">", start);
                int keyend = url.IndexOf("</a>", start);

                if (attributestart < 0 || attributeend < 0 || keyend < 0)
                {

                }
                string str_attribute = url.Substring(attributestart + 6, attributeend - attributestart - 7).Trim();
                string str_value = url.Substring(attributeend + 1, keyend - attributeend - 1).Trim();
                string pageName = "/Page/LexunMap.xaml";
                pageName += "?url=" + str_attribute;
                Uri uri = new Uri(pageName, UriKind.Relative);
                Hyperlink temprun = new Hyperlink();
                temprun.Foreground=new SolidColorBrush(Common.HtmlToColor("#FF7A9AB7"));
                Run run = new Run();
                run.Text = str_value;
                temprun.Inlines.Add(run);
                temprun.NavigateUri = uri;
                parag.Inlines.Add(temprun);
                int start1 = url.IndexOf("<a", keyend);
                if (start1 < 0)
                {
                    start = start1;
                }
                else
                {
                    Run endrun = new Run();
                    endrun.Foreground = Theme.cuTheme.SecondTextBrush;
                    endrun.FontSize = 24;
                    endrun.Text = (url.Substring(keyend + 4, start1 - keyend - 4)).Trim();
                    parag.Inlines.Add(endrun);
                    start = start1;
                }
            }
            //return dic;
        }

    }
}
